package erp_services

import (
	"xiehui-service/app/models/erp_models"
	"xiehui-service/global"
)

type paramConfigService struct {
}

var ParamConfigService = new(paramConfigService)

// 查询配置信息
func (paramConfigService *paramConfigService) GetParamByKey(k string, station_id string, mixer_no string) (pc *erp_models.ParamConfig, err error) {
	err = global.App.DB.Where("status = ? AND param_key = ? AND station_id = ? AND mixer_no = ?", 1, k, station_id, mixer_no).First(&pc).Error
	return
}

func (paramConfigService *paramConfigService) SetParamByKey(k, val string, station_id string, mixer_no string) (pc *erp_models.ParamConfig, err error) {
	res := global.App.DB.Where("status = ? AND param_key = ? AND station_id = ? AND mixer_no = ?", 1, k, station_id, mixer_no).First(&pc)

	if res.RowsAffected > 0 {
		pc.ParamValue = val
		global.App.DB.Save(&pc)
	}

	return
}

// 新增配置参数
func (paramConfigService *paramConfigService) AddParamByKey(k string, v string, station_id string, mixer_no string) (pc *erp_models.ParamConfig, err error) {
	pc = &erp_models.ParamConfig{ParamKey: k, ParamValue: v, StationId: station_id, MixerNo: mixer_no, Status: 1}

	err = global.App.DB.Create(&pc).Error
	return
}
